class MainScene extends eui.Component {
    public group_enter: eui.Group;
    public input_num: eui.TextInput;
    public btn_enter: eui.Button;
    public group_info: eui.Group;
    public group: eui.Group;
    public btn_change: eui.Button;

    private inputNum: number = 0;

    private static instance: MainScene;

    private socket: egret.WebSocket = new egret.WebSocket();

    constructor() {
        super();
        MainScene.instance = this;
        this.skinName = 'resource/eui_skins/Skins/MainSceneSkin.exml';

        this.socket.type = egret.WebSocket.TYPE_BINARY;
        this.socket.connect('localhost', 3001);
        this.socket.addEventListener(egret.Event.COMPLETE, this.onSucess, this);
        this.socket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.dataHandler, this);
        this.socket.addEventListener(egret.Event.CLOSE, this.closeHandler, this);
    }

    createChildren() {
        super.createChildren();
        this.btn_enter.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBtnEnter, this)
        this.btn_change.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBtnChange, this)
        this.btn_change.label = "摇一摇"

        this.group_enter.visible = true;
        this.group_info.visible = false;
    }

    private onBtnEnter() {
        this.inputNum = +this.input_num.text;
        this.group_enter.visible = false;
        this.group_info.visible = true;
    }

    private onBtnChange() {
        this.group.removeChildren();
        for (let i = 0; i < this.inputNum; i++) {
            let element = new Dice();
            this.group.addChild(element);
            element.update();
        }

        console.log('发送消息！！！');
        let bytes = new egret.ByteArray();
        bytes.writeUTFBytes(Math.ceil(Math.random() * 12) + '');
        this.onSend(bytes);
    }

    onSend(data: egret.ByteArray) {
        data.position = 0;
        this.socket.writeBytes(data, 0, data.bytesAvailable);
    }

    sendHeart(cmd: number = 10006) {
        let byte = new egret.ByteArray();
        byte.endian = 'littleEndian'
        byte.writeByte(81)//Q ASCII可显示字符
        byte.writeByte(83)//S
        byte.writeShort(0);
        byte.writeShort(cmd);
        this.socket.writeBytes(byte, 0, byte.bytesAvailable);
        return byte
    }

    private onSucess() {
        console.warn('连接成功！！！')
    }

    private dataHandler(data: egret.ProgressEvent) {
        // let bytes = new egret.ByteArray();
        // this.socket.readBytes(bytes);

        var b = new Blob([data.target._readMessage], { type: 'text/plain' });
        var r = new FileReader();
        r.readAsArrayBuffer(b);
        r.onload = function () {
            console.log('收到消息:', r.result);
        }


    }

    private getUint8Array(byte: egret.ByteArray): Array<number> {
        let data: Array<number> = [];
        for (let i: number = 0; i < byte.dataView.byteLength; ++i) {
            data.push(byte.dataView.getUint8(i));
        }
        return data;
    }

    closeHandler() {
        alert(`关闭通信`);
    }
}